package com.kindergarten.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kindergarten.pojo.Classes;
import com.kindergarten.pojo.Notice;
import com.kindergarten.pojo.Sign;
import com.kindergarten.pojo.User;
import com.kindergarten.service.ISignService;
import com.kindergarten.service.IUserService;
import com.kindergarten.utils.DataResults;
import com.kindergarten.utils.DateUtils;
import com.kindergarten.utils.MMGridPageVoBean;
import com.kindergarten.utils.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author bruce
 * @since 2022-10-28
 */
@Controller
@RequestMapping("/sign-api")
public class SignController {

    @Autowired
    ISignService signService;

    @Autowired
    IUserService iUserService;

    /**
     * 01-跳转到模板引擎页面
     *
     * @return
     */
    @GetMapping("topage")
    public String topage() {
        return "daKa";
    }

    /**
     * 打卡标准时间
     */
    @Value("${startTime}")
    String startTime="8:00:00";  //默认值

    /**
     * 下班标准时间
     */
    @Value("${endTime}")
    String endTime="17:00:00";  //默认值

    /**
     * 02-签到
     * @return
     */
    @PostMapping("addSign")
    @ResponseBody
    public DataResults addSign(Sign sign, HttpSession session){
        User u= (User) session.getAttribute("user");
        sign.setKqrId(u.getUserId());
        sign.setKqrType(u.getUserState());

        sign.setSignIn(DateUtils.now());//考勤时间

        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");

        String time = formatter.format(date).split(" ")[2]; //上午  下午
        String time1 = formatter.format(date).split(" ")[1]; // 10:22:11

        if(time.equals("上午") && time1.compareTo(startTime)>0) {
            sign.setState(1);
        }else {
            sign.setState(3);
        }
        return DataResults.success(ResultCode.SUCCESS, signService.save(sign));
    }

    /**
     * 02-签退
     * @return
     */
    @PostMapping("addQianTui")
    @ResponseBody
    public DataResults addQianTui(Sign sign,HttpSession session){

        User u= (User) session.getAttribute("user");
        sign.setKqrId(u.getUserId());
        sign.setKqrType(u.getUserState());

        sign.setSignIn(DateUtils.now());//考勤时间

        Date date=new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss a");
        String time = formatter.format(date).split(" ")[2];
        String time1 = formatter.format(date).split(" ")[1];

        if(time.equals("下午") && time1.compareTo(endTime)<0) {
            sign.setState(2); //早退
        }else{
            sign.setState(1);
        }
        return DataResults.success(ResultCode.SUCCESS, signService.save(sign));
    }



}
